window.onload = function() {
	var client_2 = document.getElementById("client_2");
	var dialog_border_2 = document.getElementById("dialog_border_2");
	var navHeadimg = document.getElementsByClassName("navHeadimg")[0]
	var navAfterLoginBox = document.getElementsByClassName("navAfterLoginBox")[0]
	client_2.onmouseover = function() {
		dialog_border_2.style.display = "inline-block";
	};
	client_2.onmouseout = function() {
		dialog_border_2.style.display = "none";
	};
	dialog_border_2.onmouseover = function() {
		dialog_border_2.style.display = "inline-block";
	};
	dialog_border_2.onmouseout = function() {
		dialog_border_2.style.display = "none";
	};
	navHeadimg.onmouseover = function() {
		navAfterLoginBox.style.display = "inline-block";
	};
	navHeadimg.onmouseout = function() {
		navAfterLoginBox.style.display = "none";
	};
	navAfterLoginBox.onmouseover = function() {
		navAfterLoginBox.style.display = "inline-block";
	};
	navAfterLoginBox.onmouseout = function() {
		navAfterLoginBox.style.display = "none";
	};
	course_Id = GetUrlByParamName("courseId")
	getCourseDetailByCourseId(course_Id)
	getCourseComment()
	getCookie()
}
$(document).ready(function() {
	$('[data-toggle="tooltip"]').tooltip();
});

function switchLoginType(a) {
	var hetaoCode = document.getElementById("hetaoCode")
	var hetaoAccount = document.getElementById("hetaoAccount")
	if(a.parentElement == hetaoAccount) {
		hetaoCode.style.display = "block"
		hetaoAccount.style.display = "none"
	} else if(a.parentElement == hetaoCode) {
		hetaoCode.style.display = "none"
		hetaoAccount.style.display = "block"
	}
}

function changePage(a) {
	var detailTitle = document.getElementsByClassName("detailTitle")[0]
	var detailComment = document.getElementsByClassName("detailComment")[0]
	var courseDetail = document.getElementsByClassName("courseDetail")[0]
	var pinglunBox = document.getElementsByClassName("pinglunBox")[0]
	if(a == true) {
		courseDetail.style.display = "block"
		pinglunBox.style.display = "none"
		detailTitle.classList.add("detailTitleBeSelected")
		detailComment.classList.remove("detailTitleBeSelected")
	} else {
		courseDetail.style.display = "none"
		pinglunBox.style.display = "block"
		detailComment.classList.add("detailTitleBeSelected")
		detailTitle.classList.remove("detailTitleBeSelected")
	}
}

var user_token
var user_id
var course_Id
var courseDetail
var pageNum = 1
var start
var end
var totalPage
//控制第X次开课选项div的显示与消失
//function OpenOrCloseOptions() {
//	var options = document.getElementById("options")
//	var icon = document.getElementById("listIcon");
//	var showOption = document.getElementById("showOption")
//	if($("#options").css("display") == "none") {
//		options.style.display = "block"
//		icon.classList.remove("fa-chevron-down")
//		icon.classList.add("fa-chevron-up");
//	} else if($("#options").css("display") == "block") {
//		options.style.display = "none"
//		icon.classList.remove("fa-chevron-up");
//		icon.classList.add("fa-chevron-down")
//	}
//	$(document).one("click",
//		function() { //对document绑定一个影藏Div方法
//			$("#options").hide();
//			icon.classList.remove("fa-chevron-up");
//			icon.classList.add("fa-chevron-down")
//		});
//
//	event.stopPropagation(); //阻止事件向上冒泡
//}
//
//$(".classTimeSelectBox").click(function(event) {
//	event.stopPropagation(); //阻止事件向上冒泡
//});
//function goto(id) {
//	var time = document.getElementById("time")
//	var beSelectedOption = document.getElementById(id)
//	var showOption = document.getElementById("showOption")
//	var targetTitle = beSelectedOption.title
//	var targetText = beSelectedOption.innerText
//	var agencyTitle = showOption.title
//	var agencyText = time.innerText
//	showOption.title = targetTitle
//	time.innerText = targetText
//	beSelectedOption.title = agencyTitle
//	beSelectedOption.innerText = agencyText
//}
function GetUrlByParamName(name)   {    
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");    
	var URL =  decodeURI(window.location.search);    
	var r = URL.substr(1).match(reg);    
	if(r != null) {       //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
		return  decodeURI(r[2]);    
	};    
	return null;  
}

function quit() {
	window.event.returnValue = false
	$.cookie('login_cookie', "", {
		expires: -1,
		path: '/',
	})
	window.location.href = "http://localhost/page/LoginPage/Login.html";
}

function login() {
	var mobile = document.getElementById("inputCount").value
	var password = document.getElementById("inputPassword").value
	var data = {
		"mobile": mobile,
		"password": password,
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/login",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			if(r.msg != "登录成功") {
				alert(r.msg)
			} else {
				$.cookie('login_cookie', r.data.token.token, {
					expires: 123456789,
					path: '/',
				})
				console.log($.cookie("login_cookie"))
				user_token = r.data.token.token
				document.getElementById("gotoPersonal").href = "http://localhost/page/personalPage/personalPage.html?token=" + user_token
				queryPersonalMessage(r.data.token.token);
				login_hide()
			}
		},
		error: function() {
			alert("error");
		}
	});
}

function getCookie() {
	var loginBoolean = $.cookie('login_cookie');
	if(loginBoolean) {
		//JSON.parse()  将字符串解析为对象，方便使用
		user_token = loginBoolean
		document.getElementById("gotoPersonal").href = "http://localhost/page/personalPage/personalPage.html?token=" + user_token
		login_hide();
		queryPersonalMessage(user_token);
	} else {}
}

function queryPersonalMessage(a) {
	var data = {
		"token": a,
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/personal/getUserAndCount?token=" + a,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			user_id = r.data.userId
			document.getElementById("my-img").src = r.data.headImg
		},
		error: function() {
			alert("error");
		}
	});
}

function login_hide() {
	var navBeforeLogin = document.getElementById("navBeforeLogin")
	var navAfterLogin = document.getElementById("navAfterLogin")
	navBeforeLogin.style.display = "none"
	navAfterLogin.style.display = "inline-block"
	$('#exampleModal').modal('hide')
}

function getCourseDetailByCourseId(courseId) {
	$.ajax({
		type: "get",
		url: "http://localhost/zte-api/api/course/pcGetCourseById?courseId=" + courseId,
		contentType: "application/json",
		success: function(r) {
			courseDetail = r.data
			console.log(courseDetail)
			setCourseMessage()
		},
		error: function() {
			alert("error");
		}
	});
}

function setCourseMessage() {
	var courseCover = document.getElementById("courseCover")
	var courseTitle = document.getElementById("courseTitle")
	var talkTeacher = document.getElementById("talkTeacher")
	var courseTypeName = document.getElementById("courseTypeName")
	var courseLevel = document.getElementById("courseLevel")
	var courseCollectionPeople = document.getElementById("courseCollectionPeople")
	var courseMustKnow = document.getElementById("courseMustKnow")
	var courseIntroduce = document.getElementById("courseIntroduce")
	//	课程封面
	courseCover.src = courseDetail.coverPath
	//	课程标题
	courseTitle.innerText = courseDetail.title
	//	讲师姓名
	talkTeacher.innerText = courseDetail.teacherName
	//	课程分类
	courseTypeName.innerText = courseDetail.name
	//	课程等级
	courseLevel.innerText = courseDetail.level
	//	课程收藏人数
	courseCollectionPeople.innerText = courseDetail.collectionCount
	//	课程须知
	if(courseDetail.courseMustKnow != null && courseDetail.courseMustKnow != "") {
		courseMustKnow.innerText = courseDetail.courseMustKnow
	} else {
		courseMustKnow.innerText = "当前课程无课程须知!"
	}

	//	课程介绍
	if(courseDetail.introduce != null && courseDetail.introduce != "") {
		courseIntroduce.innerHTML = '<p>&nbsp;&nbsp;&nbsp;' + courseDetail.introduce + '</p>'
	} else {
		courseIntroduce.innerHTML = "当前课程无课程须知!"
	}
	//	课程列表
	var courseChildBody = ""
	for(var i = 0; i < courseDetail.childBody.length; i++) {
		courseChildBody += '<p><span style="color:#333333"><span style="font-size: 16px;">' + courseDetail.childBody[i].chapterName + '</span></span></p>'
		if(courseDetail.childBody[i].childBody != null) {
			for(var f = 0; f < courseDetail.childBody[i].childBody.length; f++) {
				courseChildBody += '<p><span style="color:#999999"><span style="font-size: 13px;">' + courseDetail.childBody[i].childBody[f].chapterName + '</span></span></p>'
			}
		}
		courseChildBody += '<p><br></p>'
	}
	document.getElementById("courseList").innerHTML = courseChildBody
}

function getCourseComment() {
	var data = {
		"pageNum": pageNum,
		"pageSize": 10,
		"courseId": course_Id
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/course/getCourseCommentByCourseIdPc",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			setCourseComment(r.commentList.list)
			start = r.commentList.start
			end = r.commentList.end
			totalPage = r.commentList.totalPage
			createPageBox()
		},
		error: function() {
			alert("error");
		}
	});
}

function createPageBox() {
	var pageBox = document.getElementById("pageBox")
	var noComment = document.getElementById("AllcommentList")
	var option = ""
	if(end != 0) {
		option += '<li class="PageUpBox">'
		option += '<a onclick="changePageBox(1)" class="PageUp">第一页</a>'
		option += '</li>'
		for(var i = start; i <= end; i++) {
			if(i == pageNum) {
				option += '<li class="whichPage">'
				option += '<span class="PageBeSelected" id="' + i + '">' + i + '</span>'
				option += '</li>'
			} else {
				option += '<li class="whichPage">'
				option += '<a onclick="changePageBox(this.id)" id="' + i + '">' + i + '</a>'
				option += '</li>'
			}
		}
		option += '<li class="PageDownBox">'
		option += '<a onclick="changePageBox(' + totalPage + ')" class="PageDown">末尾页</a>'
		option += '</li>'
		pageBox.innerHTML = option
	} else {
		option += '<div style="width: 100%;height: 300px;line-height: 300px;color: #999;font-size: 18px;text-align:center;">'
		option += '<span>当前课程无评论</span>'
		option += '</div>'
		noComment.innerHTML = option
		pageBox.style.display = "none"
	}

}

function changePageBox(id) {
	pageNum = parseInt(id)
	getCourseComment()
}

function setCourseComment(commentList) {
	document.getElementById("review-tag-num").innerText = "（" + commentList.length + ")"
	var str = ""
	for(var i = 0; i < commentList.length; i++) {
		str += '<div class="commentItem">'
		str += '<div class="commentHeadImgBox">'
		str += '<a><img id="userHeadImg" style="border-radius: 50%;" width="40" height="40" src="' + commentList[i].userHeadImage + '"></a>'
		str += '</div>'
		str += '<div class="commentItemBody">'
		str += '<div class="commentUserInfo">'
		str += '<div style="margin-bottom: 9px;">'
		str += '<a class="commentUserName">' + commentList[i].userName + '</a>'
		str += '</div>'
		str += '<div class="itemBodyContent">'
		str += '<span>' + commentList[i].content + '</span>'
		str += '</div>'
		str += '<div class="commentInfo">'
		str += '<div class="commentInfoTime">' + commentList[i].createTime + '</div>'
		str += '</div>'
		//		str += '<div class="commentInfoAction">'
		//		str += '<span style="color: white;font-size: 12px;line-height: 30px;">删除</span>'
		//		str += '</div>'
		str += '</div>'
		str += '</div>'
		str += '</div>'
		str += '</div>'
	}
	document.getElementById("AllcommentList").innerHTML = str
}
//
//function deleteComment(commentId) {
//	var data = {
//		"courseId": course_Id,
//		"token": user_token,
//		"commentId": commentId
//	}
//	$.ajax({
//		type: "post",
//		url: "http://localhost/zte-api/api/course/delCourseComment?token=" + user_token,
//		contentType: "application/json",
//		data: JSON.stringify(data),
//		success: function(r) {
//			console.log(r)
//		},
//		error: function() {
//			alert("error");
//		}
//	});
//}

function gotoStudy() {
	if(user_token) {
		window.open("http://localhost/page/coursePage/studyPage.html?courseId=" + course_Id)
	} else {
		alert("请先登录!")
	}
}